import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/components/login.vue')
  },
  {
    path: '/home',
    redirect: '/welcome',
    component: () => import('@/components/Home.vue'),
    children: [{
        path: '/welcome',
        component: () => import('@/components/welcome.vue')
      }, {
        path: '/users',
        component: () => import('@/components/users/User.vue')
      },
      {
        path: '/rights',
        component: () => import('@/components/power/rights.vue')
      },
      {
        path: '/roles',
        component: () => import('@/components/power/roles.vue')
      },
      {
        path: '/params',
        component: () => import('@/components/goods/params.vue')
      }
    ]
  }
]
const router = new VueRouter({
  routes
})

// 挂载路由导航
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const tokens = window.sessionStorage.getItem('token')
  if (!tokens) return next('/login')
  next()
})
export default router
